package org.dromara.patrol.controller;

import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.patrol.domain.bo.PatrolSnapPlanBo;
import org.dromara.patrol.domain.bo.PatrolSnapQuery;
import org.dromara.patrol.domain.vo.PatrolSnapPlanVo;
import org.dromara.patrol.service.IPatrolSnapPlanService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.TableDataInfo;

import java.util.Set;

/**
 * 巡店管理-巡店抓拍配置
 * 前端访问路由地址为:/patrol/snapPlan
 *
 * @author LionLi
 * @date 2024-03-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/snap/plan")
public class PatrolSnapPlanController extends BaseController {

    private final IPatrolSnapPlanService patrolSnapPlanService;

    /**
     * 查询巡店管理-巡店抓拍配置列表
     */
//    @SaCheckPermission("patrol:snapPlan:list")
    @PostMapping("/list")
    public TableDataInfo<PatrolSnapPlanVo> list(@RequestBody PatrolSnapQuery query) {
        return patrolSnapPlanService.queryPageList(query);
    }

    /**
     * 新增巡店管理-巡店抓拍配置
     */
    @SaCheckPermission("patrol:snapPlan:add")
    @Log(title = "巡店管理-巡店抓拍配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatrolSnapPlanBo bo) {
        return toAjax(patrolSnapPlanService.insertByBo(bo));
    }

    /**
     * 获取巡店管理-巡店抓拍配置详细信息
     *
     * @param planId 主键
     */
//    @SaCheckPermission("patrol:snapPlan:query")
    @GetMapping("/{planId}")
    public R<PatrolSnapPlanVo> getInfo(@PathVariable Long planId) {
        return R.ok(patrolSnapPlanService.queryById(planId));
    }

    /**
     *抓拍巡店图片集合
     * @param query
     * @return
     */
//    @SaCheckPermission("patrol:snapPlan:list")
    @PostMapping("/imagList")
    public TableDataInfo<PatrolSnapPlanVo> imagList(@RequestBody PatrolSnapQuery query, PageQuery pageQuery) {
        return patrolSnapPlanService.queryPageImagList(query,pageQuery);
    }

    /**
     * 修改巡店管理-巡店抓拍配置
     */
    @SaCheckPermission("patrol:snapPlan:edit")
    @Log(title = "巡店管理-巡店抓拍配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatrolSnapPlanBo bo) {
        return toAjax(patrolSnapPlanService.updateByBo(bo));
    }

    /**
     * 删除巡店管理-巡店抓拍配置
     *
     * @param planId 主键
     */
    @SaCheckPermission("patrol:snapPlan:remove")
    @Log(title = "巡店管理-巡店抓拍配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planId}")
    public R<Void> remove(@PathVariable Long planId) {
        return toAjax(patrolSnapPlanService.deleteById(planId));
    }

    /**
     * 获取单日预计抓拍图片数
     * @return
     */
    @SaCheckPermission("patrol:snapPlan:total")
    @PostMapping("/total")
    public R<Long> total(@RequestBody PatrolSnapQuery query) {
        return R.ok(patrolSnapPlanService.queryTotal(query.getStoreIds()));
    }

    /**
     * 更改计划状态
      * @param status
     * @return
     */
    @SaCheckPermission("patrol:snapPlan:status")
    @Log(title = "巡店管理-巡店抓拍配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/status/{planId}")
    public R<Void> status(@PathVariable Long planId, @NotBlank(message = "状态不能为空") String status) {
        return toAjax(patrolSnapPlanService.updateStatus(planId, status));
    }

    /**
     * 获取已配置门店数量
     * @param query
     * @return
     */
    @SaCheckPermission("patrol:snapPlan:exist")
    @PostMapping("/store/exist")
    public R<Long> exist(@RequestBody PatrolSnapQuery query) {
        return R.ok(patrolSnapPlanService.queryExistByStoreIds(query.getStoreIds()));
    }

    /**
     * 获取已配置设备ids
     * @return
     */
    @GetMapping("/device/exist")
    public R<Set<Long>> exist() {
        return R.ok(patrolSnapPlanService.queryExistedDeviceIds());
    }
}
